{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "03e76146-9229-4d09-b14b-9af1f2a5a1fd",
   "metadata": {},
   "source": [
    "Python Cookbook, 3rd Ed.\n",
    "\n",
    "Chapter 14, Application Integration: Combination\n",
    "Markov Summary\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cd44f942-0dbe-4154-8e27-abe946b05dff",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "import csv\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d98e245e-8ebd-4f59-9f75-3c1bfcea7aac",
   "metadata": {},
   "outputs": [],
   "source": [
    "outcome_counts = Counter()\n",
    "lengths = {\n",
    "    'Fail': Counter(),\n",
    "    'Success': Counter(),\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fe3d9ed0-03f1-4575-9d52-d6714e2748ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# file = \"sample.csv\"\n",
      "# samples = 100\n",
      "# randomize = 42\n",
      "# file = \"../../data/ch14/data.csv\"\n",
      "# samples = 10\n",
      "# randomize = 0\n"
     ]
    }
   ],
   "source": [
    "for source in Path(\"../../data/ch14\").glob(\"*.csv\"):\n",
    "    with source.open() as source_file:\n",
    "        line_iter = iter(source_file)\n",
    "        # Skip past the header\n",
    "        for line in line_iter:\n",
    "            if \"-----\" in line:\n",
    "                break\n",
    "            print(line.rstrip())\n",
    "        reader = csv.DictReader(line_iter)\n",
    "        for sample in reader:\n",
    "            outcome_counts[sample['outcome']] += 1\n",
    "            lengths[sample['outcome']][sample['length']] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "16c373bd-668f-461d-9a41-4078f4a5e42e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Success': 58, 'Fail': 52}\n",
      "('Fail', {'2': 15, '5': 7, '4': 2, '1': 13, '3': 7, '7': 2, '6': 1, '8': 2, '11': 1, '10': 1, '9': 1})\n",
      "('Success', {'1': 27, '3': 7, '2': 7, '7': 2, '6': 4, '4': 4, '12': 1, '5': 3, '8': 2, '14': 1})\n"
     ]
    }
   ],
   "source": [
    "print(dict(outcome_counts))\n",
    "for k, v in lengths.items():\n",
    "    print((k, dict(v)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9cba9e6c-4cba-4f78-868e-83f709da8666",
   "metadata": {},
   "outputs": [],
   "source": [
    "def tabulate(label: str, value: str, mapping: dict[str, int]) -> None:\n",
    "    hline2 = f\"+{'='*10}+{'='*10}+\"\n",
    "    hline1 = f\"+{'-'*10}+{'-'*10}+\"\n",
    "\n",
    "    print(hline2)\n",
    "    print(f\"| {label:^8s} | {value:^8s} |\")\n",
    "    for k, v in sorted(mapping.items()):\n",
    "        print(hline1)\n",
    "        print(f\"| {str(k):<8s} | {v:>8d} |\")\n",
    "    print(hline2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a9227a0a-7171-4e12-9962-31cf68541a0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+==========+==========+\n",
      "| Outcome  |  Count   |\n",
      "+----------+----------+\n",
      "| Fail     |       52 |\n",
      "+----------+----------+\n",
      "| Success  |       58 |\n",
      "+==========+==========+\n"
     ]
    }
   ],
   "source": [
    "tabulate(\"Outcome\", \"Count\", outcome_counts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "21bdd993-08ea-4b02-b7d0-3da370eb6a16",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "## Fail Chains\n",
      "+==========+==========+\n",
      "|   len    |  Count   |\n",
      "+----------+----------+\n",
      "| 1        |       13 |\n",
      "+----------+----------+\n",
      "| 10       |        1 |\n",
      "+----------+----------+\n",
      "| 11       |        1 |\n",
      "+----------+----------+\n",
      "| 2        |       15 |\n",
      "+----------+----------+\n",
      "| 3        |        7 |\n",
      "+----------+----------+\n",
      "| 4        |        2 |\n",
      "+----------+----------+\n",
      "| 5        |        7 |\n",
      "+----------+----------+\n",
      "| 6        |        1 |\n",
      "+----------+----------+\n",
      "| 7        |        2 |\n",
      "+----------+----------+\n",
      "| 8        |        2 |\n",
      "+----------+----------+\n",
      "| 9        |        1 |\n",
      "+==========+==========+\n",
      "\n",
      "## Success Chains\n",
      "+==========+==========+\n",
      "|   len    |  Count   |\n",
      "+----------+----------+\n",
      "| 1        |       27 |\n",
      "+----------+----------+\n",
      "| 12       |        1 |\n",
      "+----------+----------+\n",
      "| 14       |        1 |\n",
      "+----------+----------+\n",
      "| 2        |        7 |\n",
      "+----------+----------+\n",
      "| 3        |        7 |\n",
      "+----------+----------+\n",
      "| 4        |        4 |\n",
      "+----------+----------+\n",
      "| 5        |        3 |\n",
      "+----------+----------+\n",
      "| 6        |        4 |\n",
      "+----------+----------+\n",
      "| 7        |        2 |\n",
      "+----------+----------+\n",
      "| 8        |        2 |\n",
      "+==========+==========+\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"## Fail Chains\")\n",
    "tabulate(\"len\", \"Count\", lengths[\"Fail\"])\n",
    "print()\n",
    "print(\"## Success Chains\")\n",
    "tabulate(\"len\", \"Count\", lengths[\"Success\"])\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d36573f0-fc8d-48a4-8bca-2a95740655bd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f495bb7f-aeb5-42f1-97e7-d4c7065f221d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
